include "apt"

# 安装基础软件包
class install_base {

  # 添加java私有源
  apt::ppa { ["ppa:webupd8team/java"]: }
  -> # 安装必要的软件包
  package { ["vim", "curl", "git", "expect", "bash", "zsh"]:
    ensure  => present,
    require => Exec['apt_update']
  }

  # 安装ZSH
  include 'ohmyzsh'
  ohmyzsh::install { 'vagrant':
    require => [Exec["apt_update"], Package['git']]
  }
}

# 安装java环境
class install_java {
  # 安装java8，并自动接受协议
  package { ["oracle-java8-installer"]:
    ensure  => present,
    require => Exec['accept_license']
  }

  # 接受Oracle协议
  exec { "accept_license":
    command   => "echo debconf shared/accepted-oracle-license-v1-1 select true | sudo debconf-set-selections && echo debconf shared/accepted-oracle-license-v1-1 seen true | sudo debconf-set-selections",
    cwd       => "/home/vagrant",
    user      => "vagrant",
    path      => "/usr/bin/:/bin/",
    logoutput => true,
  }
}

# 安装Tomcat
class install_tomcat {
  class { 'tomcat':
    catalina_home => '/opt/tomcat8',
    manage_user   => false,
    manage_group  => false,
    user          => "vagrant",
    group         => "vagrant",
  }

  include 'tomcat'

  # 安装tomcat实例
  tomcat::instance { 'default':
    source_url    => 'https://www.apache.org/dist/tomcat/tomcat-8/v8.0.33/bin/apache-tomcat-8.0.33.tar.gz'
  }
  -> # 配置JMX和Debug
  file { '/opt/tomcat8/bin/setenv.sh':
    ensure  => present,
    source  => 'puppet:///modules/bash_profiles/setenv.sh',
    owner   => 'vagrant',
    group   => 'vagrant',
    mode    => 755
  }
  -> # 配置部署用目录
  file { '/home/vagrant/deploy':
    ensure => 'directory',
    owner   => 'vagrant',
    group   => 'vagrant'
  }
  -> # 配置service
  file { '/etc/init.d/tomcat8':
    ensure  => present,
    source  => 'puppet:///modules/bash_profiles/tomcat8',
    owner   => 'root',
    group   => 'root',
    mode    => 755
  }
  ~>
  service { 'tomcat8':
    ensure  => running,
    enable  => true
  }
}

# 安装MySql
class install_mysql {
  class { '::mysql::server':
    override_options => {
      'mysqld'                  => {
        'bind-address'          => '0.0.0.0',
      }
    }
  }

  include '::mysql::server'

  # 先执行apt_update
  Exec['apt_update']
  -> # 安装MySql Client
  Class['::mysql::client']
  -> # 执行安装
  Class['::mysql::server']
  -> # 创建mysql数据库
  mysql::db { 'aftkd':
    user     => 'aftkd',
    password => 'aftkd',
    host     => 'localhost',
    grant    => ['ALL'],
  }
  -> # 创建可全局访问的root账号
  mysql_user { 'root@%':
    ensure                   => 'present',
    max_connections_per_hour => '0',
    max_queries_per_hour     => '0',
    max_updates_per_hour     => '0',
    max_user_connections     => '0',
  }
  ~>
  mysql_grant { 'root@%/*.*':
    ensure     => 'present',
    options    => ['GRANT'],
    privileges => ['ALL'],
    table      => '*.*',
    user       => 'root@%',
  }
}

include 'install_base'
include 'install_java'
include 'install_mysql'
include 'install_tomcat'

Class['install_base'] ->
Class['install_java'] ->
Class['install_mysql'] ->
Class['install_tomcat']